<?php
class MTemplate extends MSingleton {
    private $_default = 'default';
    private $_defaultExtension = '.php';
    private $_module;
    private $_path;
    private $_template = 'index';
    private $_scripts;
    private $_stylesheets;
    private $_vars;

    protected function  __construct() {
        parent::__construct();

        $this->_scripts = array();
        $this->_stylesheets = array();
        $this->_vars = array();
    }

    /**
     * @return String : The name of the default template directory
     */
    public function getDefault() {
        return $this->_default;
    }

    public function setDefault($_default) {
        $this->_scripts = array();
        $this->_stylesheets = array();
        $this->_default = $_default;
    }

    /**
     * @return String : The default template extension
     */
    public function getDefaultExtension() {
        return $this->_defaultExtension;
    }

    public function setDefaultExtension($_defaultExtension) {
        $this->_defaultExtension = $_defaultExtension;
    }

    /**
     * @return String : The module 
     */
    public function getModule() {
        return $this->_module;
    }

    public function setModule($_module) {
        $this->_scripts = array();
        $this->_stylesheets = array();
        $this->_module = $_module;
    }

    /**
     * @return String : The path of the template instance
     */
    public function getPath() {
        return $this->_path;
    }

    public function setPath($_path) {
        $this->_path = $_path;
    }

    /**
     * @return String : The current template file
     */
    public function getTemplate() {
        return $this->_template;
    }

    public function setTemplate($_template) {
//        $this->_scripts = array();
//        $this->_stylesheets = array();
        $this->_template = $_template;
    }

    /**
     * Add a script to the template
     * @param String $scriptSrc : Relative path to the script
     */
    public function addScript($scriptSrc) {
        if (!in_array($scriptSrc, $this->_scripts)){
            array_push($this->_scripts, $scriptSrc);
        }
    }

    /**
     * Add a stylesheet to the template
     * @param String $stylesheetSrc : Relative path to the stylesheet
     */
    public function addStylesheet($stylesheetSrc) {
        if (!in_array($stylesheetSrc, $this->_stylesheets)){
            array_push($this->_stylesheets, $stylesheetSrc);
        }
    }
    
    /**
     * @return Array : All scripts relative path
     */
    public function getScripts() {
        return $this->_scripts;
    }

    /**
     * @return Array : All stylesheets relative path
     */
    public function getStylesheets() {
        return $this->_stylesheets;
    }
    
    public function setStylesheets($stylesheets) {
        $this->_stylesheets = $stylesheets;
    }
    
    public function setScripts($scripts) {
        $this->_scripts = $scripts;
    }

    /**
     * Render XHTML Code for scripts
     * @return String : XHTML Code for the scripts
     */
    public function renderScripts(){
        $return = NULL;
        foreach ($this->_scripts as $script){
            $return .= MHtml::tag('script', array('type'=>'text/javascript', 'src'=>$script), NULL);
        }
        return $return;
    }

    /**
     * Render XHTML Code for stylesheets
     * @return String : XHTML Code for the stylesheets
     */
    public function renderStylesheets(){
        $return = NULL;
        foreach ($this->_stylesheets as $style){
            $return .= MHtml::tag('link', array('rel'=>'stylesheet', 'type'=>'text/css', 'href'=>$style));
        }
        return $return;
    }

    /**
     * @return String : Absolute path to the template
     */
    public function getLayoutPath(){
        return BASE_PATH.DS.Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'templates'.DS.$this->_default.DS.$this->_template.$this->_defaultExtension;
    }
    
    public function getAbsoluteDirectoryPath(){
        return BASE_PATH.DS.Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'templates'.DS.$this->_default;
    }

    /**
     * @return String : Relative path to the template
     */
    public function getRelativeFolderPath(){
        return Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'templates'.DS.$this->_default;
    }

    /**
     * Render the current template
     */
    public function render(){
        require (BASE_PATH.DS.Mandrill::getApp()->modulesFolder.DS.$this->_module.DS.'templates'.DS.$this->_default.DS.$this->_template.$this->_defaultExtension);
    }
    
    /**
     * Add a variable to the template
     * @param String $name : Name of the variable
     * @param Mixed $value : Value for the variable
     */
    public function addVar($name, $value){
        $this->_vars[$name] = $value;
    }
    
    /**
     * Remove all defined variables
     */
    public function clearVars(){
        $this->_vars = array();
    }
    
    /**
     * @return Array : All defined variables
     */
    public function getVars(){
        return $this->_vars;
    }
    
    /**
     * Automatically add stylesheets in folder named "css" in template folder
     */
    public function publishAttachedStylesheets(){
        if (file_exists($this->getAbsoluteDirectoryPath().DS.'css')){
            MPublisher::publish($this->getAbsoluteDirectoryPath().DS.'css');
            $matchedFiles = glob($this->getAbsoluteDirectoryPath().DS.'css'.DS.'*.css');
            foreach ($matchedFiles as $css){
                $this->addStylesheet(__publish($css));
            }
        }
    }
    
    /**
     * Automatically add scripts in folder named "js" in template folder
     */
    public function publishAttachedScripts(){
        if (file_exists($this->getAbsoluteDirectoryPath().DS.'js')){
            MPublisher::publish($this->getAbsoluteDirectoryPath().DS.'js');
            $matchedFiles = glob($this->getAbsoluteDirectoryPath().DS.'js'.DS.'*.js');
            foreach ($matchedFiles as $js){
                $this->addScript(__publish($js));
            }
        }
    }

    public function __destruct() {
        unset($this->_stylesheets);
        unset($this->_scripts);
        unset($this->_vars);
    }
}
?>
